package servlet;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.Customer;
import pojo.Favourite;
import service.CustomerManager;
import service.impl.CustomerManagerFactory;
import verify.MyVerify;


@WebServlet("/add")
public class AddCustomerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private CustomerManager manager;
	private DateFormat dateFormat;
	private MyVerify myVerify;
       
    public AddCustomerServlet() {
        super();
        manager = CustomerManagerFactory.getInstance();
        dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        myVerify = new MyVerify();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//接参
		String cname = request.getParameter("cname");
		String csex = request.getParameter("sex");
		String csal = request.getParameter("sal");
		String cbirth = request.getParameter("birth");
		String[] cfavs = request.getParameterValues("favs");
		System.out.println(csex);
		//校验
		String verify = verify(cname, csal, cbirth, csex,cfavs);
		if (!verify.equals(MyVerify.SUCCESS)) {
			response.sendRedirect("error?info="+verify);
			return;
		}
		
		//类型转换
		
		boolean sex = Boolean.parseBoolean(csex);
		double sal = Double.parseDouble(csal);
		Date birth = null;
		try {
			 birth  = dateFormat.parse(cbirth);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		//封装
		Customer c = new Customer(null, cname, sal, sex, birth);
		int[] favs = new int[cfavs.length];
		List<Favourite> favourites = new ArrayList<>();
		for (int i = 0; i < cfavs.length; i++) {
			System.out.println(cfavs[i]);
			favs[i] = Integer.parseInt(cfavs[i]);
			Favourite favourite = new Favourite();
			favourite.setId(favs[i]);
			favourites.add(favourite);
		}
		c.setFavs(favourites);
		//调用业务
		int m = manager.addCustomer(c);
		if (m>0) {
			//存入context
			request.setAttribute("c", c);
			//转发
			System.out.println("5555");
			response.sendRedirect("list");
		}else{
			System.out.println("6666");
			response.sendRedirect("error?info=insert fail");
		}
		
		
	}
	
	public String verify(String uname,String sal,String birth,String sex,String[] favs){
		String verify = myVerify.verifyString(uname);
		if (!MyVerify.SUCCESS.equals(verify)) {
			return "cname"+verify;
		}
		verify = myVerify.verifyDouble(sal);
		if (!MyVerify.SUCCESS.equals(verify)) {
			return "sal"+verify;
		}
		
		verify = myVerify.verifyDate(birth);
		if (!MyVerify.SUCCESS.equals(verify)) {
			return "birth"+verify;
		}
		verify = myVerify.verifyBoolean(sex);
		if (!MyVerify.SUCCESS.equals(verify)) {
			return "sex"+verify;
		}
		for (int i = 0; i < favs.length; i++) {
			verify = myVerify.verifyInteger(favs[i]);
			if (!verify.equals(MyVerify.SUCCESS)) {
				return "favs "+i+verify;
			}
		}
		return MyVerify.SUCCESS;
		
	}

}
